package com.ruoyi.ora.redtea.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ora.redtea.config.ApiConfig;
import com.ruoyi.ora.redtea.model.ProfileRequest;
import com.ruoyi.ora.redtea.model.RedteaResult;
import com.ruoyi.ora.redtea.model.UsageRequest;
import com.ruoyi.ora.redtea.service.IDeviceRedteaService;
import com.ruoyi.ora.redtea.util.SignatureUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class DeviceRedteaServiceImpl implements IDeviceRedteaService {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ApiConfig apiConfig;

    /**
     * 获取码号用量
     */
    @Override
    public RedteaResult getProfileUsage(List<String> iccids, String startDate, String endDate) throws Exception {
        String url = apiConfig.getBaseUrl() + "/api/cmp/v1/external/profile/usage";

        ObjectMapper objectMapper = new ObjectMapper();

        // 构建请求体
        UsageRequest requestBody = new UsageRequest(iccids, startDate, endDate);
        String requestBodyJson = objectMapper.writeValueAsString(requestBody);

        // 生成请求头参数
        String timestamp = String.valueOf(System.currentTimeMillis());
        String requestId = IdUtils.randomUUID();

        // 生成签名
        String data = SignatureUtil.generateRequestData(
                apiConfig.getAccessKey(),
                timestamp,
                requestId,
                requestBodyJson
        );

        String signature = SignatureUtil.generateHMACSHA256(data, apiConfig.getAccessSecret());

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("AccessKey", apiConfig.getAccessKey());
        headers.set("Timestamp", timestamp);
        headers.set("RequestId", requestId);
        headers.set("Version", "V1.0");
        headers.set("SignMethod", "HMAC-SHA256");
        headers.set("Signature", signature);

        // 创建HTTP实体
        HttpEntity<String> entity = new HttpEntity<>(requestBodyJson, headers);

        // 发送请求
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                entity,
                String.class
        );

        if (response.getStatusCode() == HttpStatus.OK) {
            System.out.println(response.getBody());
            return null;
        } else {
            throw new RuntimeException("API调用失败，状态码: " + response.getStatusCode());
        }
    }

    @Override
    public RedteaResult getProfileList(List<String> iccids, String status, Integer pageNo, Integer pagesize) throws Exception {
        String url = apiConfig.getBaseUrl() + "/api/cmp/v1/external/profile/list";

        ObjectMapper objectMapper = new ObjectMapper();

        // 构建请求体
        ProfileRequest requestBody = new ProfileRequest(iccids, status, pageNo, pagesize);
        String requestBodyJson = objectMapper.writeValueAsString(requestBody);

        // 生成请求头参数
        String timestamp = String.valueOf(System.currentTimeMillis());
        String requestId = IdUtils.randomUUID();

        // 生成签名
        String data = SignatureUtil.generateRequestData(
                apiConfig.getAccessKey(),
                timestamp,
                requestId,
                requestBodyJson
        );

        String signature = SignatureUtil.generateHMACSHA256(data, apiConfig.getAccessSecret());

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("AccessKey", apiConfig.getAccessKey());
        headers.set("Timestamp", timestamp);
        headers.set("RequestId", requestId);
        headers.set("Version", "V1.0");
        headers.set("SignMethod", "HMAC-SHA256");
        headers.set("Signature", signature);

        // 创建HTTP实体
        HttpEntity<String> entity = new HttpEntity<>(requestBodyJson, headers);

        // 发送请求
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                entity,
                String.class
        );

        if (response.getStatusCode() == HttpStatus.OK) {
            System.out.println(response.getBody());
            return null;
        } else {
            throw new RuntimeException("API调用失败，状态码: " + response.getStatusCode());
        }
    }

}
